package TestSparkConversionOperator;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

import java.util.List;

public class TestSparkTopN {

    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("testTopN").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);

        JavaRDD<String> rdd = sc.textFile("H:\\yjy\\资源\\新建文件夹\\violation.txt");

        Function<String, Integer> function = new Function<String, Integer>() {
            @Override
            public Integer call(String s) throws Exception {
                return Integer.parseInt(s.split("\t")[1]);
            }
        };


        JavaRDD<String> rdd1 = rdd.sortBy(function, false, rdd.getNumPartitions());


        List<String> res = rdd1.take(3);
        for (String i : res) {
            System.out.println(i);
        }


    }
}
